package com.kylin.system.controller;

import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.system.entity.SysDictData;
import com.kylin.system.service.SysDictDataService;
import com.kylin.system.service.SysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据字典信息
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController
{
    @Autowired
    private SysDictDataService dictDataService;
    
    @Autowired
    private SysDictTypeService dictTypeService;

//    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysDictData>> list(SysDictData dictData)
    {
        // 构造搜索
        dictData.getQueryWrapper()
                .eq(StringUtils.isNotBlank(dictData.getDictType()),"dict_type", dictData.getDictType())
                .like(StringUtils.isNotBlank(dictData.getDictLabel()),"dict_label", dictData.getDictLabel())
                .eq(StringUtils.isNotBlank(dictData.getStatus()),"status", dictData.getStatus());

        Page<SysDictData> page = new Page<>();
        page = dictDataService.listPage(page, dictData);
        return ResultBean.onSuccess(page);
    }

    /**
     * 查询字典数据详细
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public ResultBean<SysDictData> getInfo(@PathVariable Long dictCode)
    {
        // 非空校验
        Assert.isBlank("查询编号不能为空", dictCode);
        return ResultBean.onSuccess(dictDataService.getById(dictCode));
    }

    /**
     * 新增字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean<Object> add(@RequestBody SysDictData dict)
    {
        // 参数校验
        BeanValidator.validate(dict);
        // 重复性校验
        dictDataService.insertDictData(dict);

        return ResultBean.onSuccess();
    }

    /**
     * 修改保存字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean<Object> edit(@RequestBody SysDictData dict)
    {
        // 参数校验
        BeanValidator.validate(dict);
        dictDataService.updateDictData(dict);
        return ResultBean.onSuccess();
    }

    /**
     * 删除字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public ResultBean<Object> remove(@PathVariable Long[] dictCodes)
    {
        Assert.isBlank("请选择需要更新的数据", dictCodes);
        int count = dictDataService.deleteDictDataByIds(dictCodes);
        return ResultBean.onSuccess().put("count", count);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public ResultBean<List<SysDictData>> dictType(@PathVariable String dictType)
    {
        return ResultBean.onSuccess(dictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/types")
    public ResultBean<Map<String,List<SysDictData>>> dictTypes(String dictTypes)
    {
        Map<String, List<SysDictData>> dictTypeMap = Arrays.stream(dictTypes.split(","))
                .collect(Collectors.toMap(
                        item -> item,
                        item -> dictTypeService.selectDictDataByType(item)
                ));
        return ResultBean.onSuccess(dictTypeMap);
    }

}
